home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_200
/
211_01
/
xsrchr.c
< prev
next >
Wrap
C/C++ Source or Header
|
1980-01-01
|
22KB
|
1,076 lines
/*XSRCHR.C VERS:- 01.00 DATE:- 09/26/86 TIME:- 09:38:02 PM */
/*
%CC1 $1.C -O -X -E7A00
%CLINK $1 SCOPE DIO WILDEXP -F CHARFUN STRFUN -S -E7A00
%DELETE $1.CRL
*/
/*
Description:
Search over each record of each file in wildcard filelist;
if the seach pattern is found in a record, display it,
with DIO output redirection.
If the search pattern is given as the first command line parameter,
the record delimiter is assumed to be '\n'.
If the -p option is selected or the search pattern is not given on the
command line, there is full screen input of:
search patterns;
record delimiter;
output delimiters;
cas_flag;
In the case of full screen input, there can be up to 10 search patterns,
which may be or'd, and'd, or not'd.
If the cas_flag is set, the file name is printed at the start of
the first line of output of each record.
Strings can include meta characters, such as '*', '?', etc.
See help_mess for a listing of them.
Usage:
xsrch [pattern] input [>output_ufn (def = crt)] [-options (-h = help)]
The input parameter is required. It can be either:
input_afn1 [input_anf2, ..] and/or <input_ufn_list
If a pattern to be searched for is given on the command line (rather than
given through full-screen input), it must precede the input parameter.
It is best to select full screen input with the option '-p', rather than
trust to default selection.
See help_mess for more information on options.
From public domain code:
Van Nuys SEARCH, DIO, WILDEXP (by Eugene H. Mallory):
WILDEXP modified to allow du: == u/d:.
DIO modified to avoid cr-cr-lf triple with putchar.
some functions rewritten from code in SEARCH:
decode()
find()
search()
CUG SCOPE:
Full screen input based on the template file XSRCH.ZIP:
select and, not, or strings;
select parts of record for output;
select record delimiter;
select flags.
Depending on test, 1-5 x slower than FIND.COM.
Effects on efficiency:
major use of machine coded routines for character and string operations;
major use of file read() with large block size;
major inline C-code for touppper in search();
minor reduced parameter passing;
minor reduced setting of variables;
minor use of ! instead of == 0;
minor return from recursive calls of search with longjmp;
minor expand code of search(), so branching gives fewer operations/branch;
minor pointer arithmetic instead of arrays in search().
By J.A. Rupley, Tucson, Arizona
Coded for BDS C compiler, version 1.50a
*/
/* page eject */
#include "bdscio.h"
#include "dio.h"
/*configuration*/
#define READSECS 10
#define BUFLEN (((6 * READSECS) + 1) * 128)
#define DELIM "\n"
#define NO_SUMMARY FALSE
#define LIST_FLAG TRUE
#define RECORD_NUMBERS FALSE
#define INP_PATTERN FALSE
#define DISP_CTRL FALSE
#define SKIP 0
#define CAS_FLAG FALSE
/*search constants*/
#define FOUND 0x80
#define NOTFOUND 0x81
#define MATCH 0x82
#define NOTMATCH 0x83
#define NOTBEGIN 0xff
#define NOTEND 0x9d
#define SKIPANY 0x86
#define SKIPONE 0x87
#define IGNOREONE 0x1c
#define LINEEND 0x1d
#define LINESTART 0x7f
#define ANYALPHA 0x1e
#define ANYNUMBER 0x1f
/*buffers and arrays*/
#define NPAT 10
#define NDRIVER 7
#define NOUT 10
char string[BUFLEN];
char pat_buf[42];
char delim[42];
char pat_array[NPAT][42];
char out_start[NOUT][21];
char out_end[NOUT][21];
char fname[MAXLINE];
char jbuf[JBUFSIZE];
/*external constants, used for speed or control*/
int i, j, firstpat, nextpat;
int skip, no_summary, list_flag, record_numbers;
int disp_ctrl, inp_pattern, optionerr;
int cas_flag;
int npat, nout;
char *pattern;
int ioblks, iofile;
main(argc, argv)
char **argv;
int argc;
{
int ii;
int string_compare();
cas_flag = CAS_FLAG;
inp_pattern = INP_PATTERN;
record_numbers = RECORD_NUMBERS;
no_summary = NO_SUMMARY;
list_flag = LIST_FLAG;
optionerr = FALSE;
disp_ctrl = DISP_CTRL;
skip = SKIP;
dioinit(&argc, argv);
get_options(&argc, argv);
if (optionerr || ((argc < 2) && !DIOIN))
help_mess();
/*set up patterns, of all sorts*/
if (((argc < 3) && !DIOIN) || inp_pattern)
{
inp_pattern = TRUE;
full_screen();
}
else
{
strcpy(delim, DELIM);
pat_decode(argv[1], &pat_array[0][1]);
pat_array[0][0] = 'O';
npat = 1;
argv = &argv[1];
argc -= 1;
}
/* now wildcard and sort what is left on command line */
wildexp(&argc, &argv);
qsort(&argv[1], argc - 1, 2, &string_compare);
/* search filelist from command line, if any*/
for (ii = 1; ii < argc; ii++)
{
strcpy(fname, argv[ii]);
find_all(fname);
}
/* search filelist from input file, if any*/
if (DIOIN)
while (!getstring(fname))
find_all(fname);
dioflush();
exit();
}
/* END OF MAIN*/
/*page eject*/
int find_all(fname)
char *fname;
{
int c, temp;
int record;
int next, isave;
char csave, csavefirst;
char ichar;
int find_count;
int jj, kk;
int first_char, end_char;
int first_temp, end_temp;
/*fill after end of file name with spaces to column 20*/
/*then open file, print filename, and process file*/
for (jj = strlen(fname); jj; jj--)
if ((c = fname[jj - 1]) != ' ' && c != '\r' && c != '\n')
break;
while (jj < 20)
fname[jj++] = ' ';
fname[jj] = '\0';
if ((iofile = open(fname, 0)) == ERROR)
{
typef("\nFIND_ALL: cannot open file %s.\n", fname);
close(iofile);
return (0);
}
else
if (!no_summary)
printf("\f\n%s\n", fname);
find_count = 0;
record = 1;
string[0] = LINESTART;
string[1] = '\0';
if (skip)
if ((record += skip_lines(skip)) == ERROR)
{
typef(" %s.\n", fname);
close(iofile);
return (0);
}
firstpat = 0;
/*main loop to search file for pattern, record by record*/
/*first, with -pat_gets- find next record terminated by -delim- */
while ((temp = pat_gets(delim)) != ERROR)
{
/*intercept console input*/
if (bdos(11))
{
if ((c = bdos(1)) == 0x03) /*ctrl-c*/
{
typef("\nFIND_ALL: Control C intercepted\n");
dioexit();
}
else
if (c == 0x18) /*ctrl-x*/
{
typef("\nFIND_ALL: Control X intercepted\n");
close(iofile);
return (1);
}
}
/*store end characters of record */
/*replace by special delimiters for search*/
csavefirst = string[firstpat];
string[firstpat] = LINEEND;
isave = firstpat + 1;
csave = string[isave];
string[isave] = '\0';
next = nextpat;
/*with -all_pats- search record for all patterns*/
/*output as appropriate*/
if (all_pats() == FOUND)
{
if (!find_count++)
{
outs(delim, MAXLINE, disp_ctrl);
printf("\n");
}
if (list_flag)
{
if (nout)
{
if (cas_flag)
printf("%s", fname);
if (record_numbers)
printf("Record number %-7d ", record);
first_char = 1;
for (kk = 0; kk < nout; kk++)
{
firstpat = first_char;
pattern = out_start[kk];
if (find() != FOUND)
continue;
first_char = firstpat++;
pattern = out_end[kk];
if (find() != FOUND)
continue;
end_char = firstpat;
outs(&string[first_char], (end_char - first_char), disp_ctrl);
if (temp == FOUND && kk == (nout - 1))
for (jj = 0; (ichar = out_end[kk][jj]); jj++)
if (ichar == LINEEND && (out_end[kk][jj - 1] != '!'))
outs(delim, MAXLINE, disp_ctrl);
}
}
else
{
if (record_numbers)
printf("%7d ", record);
string[isave - 1] = '\0';
outs(&string[1], BUFLEN, disp_ctrl);
if (temp == FOUND)
outs(delim, BUFLEN, disp_ctrl);
}
}
/*if not list_flag, ie if select files, not lines*/
else
{
if (!no_summary)
summary_output(find_count);
close(iofile);
return (1);
}
}
record++;
/*normal exit at end of file*/
if (temp == NOTFOUND)
{
outs(delim, MAXLINE, disp_ctrl);
printf("\n");
if (!no_summary)
{
if